unit uClientes;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Vcl.Buttons, Vcl.DBCtrls,
  Vcl.ExtCtrls, Vcl.Mask, Vcl.Grids, Vcl.DBGrids, Vcl.StdCtrls, JvExStdCtrls,
  JvButton, JvCtrls, Vcl.ComCtrls, JvExMask, JvToolEdit, JvDBControls,
  JvCombobox, JvDBCombobox, JvgSpeedButton, JvExControls, JvSpeedButton;

type
  TfrmClientes = class(TForm)
    PageControlClientes: TPageControl;
    TabSheetConsulta: TTabSheet;
    GroupBox1: TGroupBox;
    Label5: TLabel;
    Label10: TLabel;
    edtNome: TEdit;
    edtCPF: TEdit;
    btnFiltrar: TJvImgBtn;
    GroupBox2: TGroupBox;
    DBGrid1: TDBGrid;
    GroupBox3: TGroupBox;
    Label2: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    DBEdit2: TDBEdit;
    DBEdit4: TDBEdit;
    DBEdit11: TDBEdit;
    DBEdit14: TDBEdit;
    DBEdit15: TDBEdit;
    DBEdit16: TDBEdit;
    DBEdit17: TDBEdit;
    DBEdit18: TDBEdit;
    Panel1: TPanel;
    DBNavigator: TDBNavigator;
    btnNovo: TJvImgBtn;
    btnEditar: TJvImgBtn;
    btnExcluir: TJvImgBtn;
    TabSheetManutencao: TTabSheet;
    DBRadioGroupTipo: TDBRadioGroup;
    Panel2: TPanel;
    btnOk: TBitBtn;
    dsTblcli: TDataSource;
    Label23: TLabel;
    Label24: TLabel;
    DBEdit19: TDBEdit;
    Label25: TLabel;
    DBEdit20: TDBEdit;
    TimerInicio: TTimer;
    GroupBox4: TGroupBox;
    lblCpfCaption: TLabel;
    Label3: TLabel;
    lblRgCaption: TLabel;
    dbedtClicpf: TDBEdit;
    DBEdit3: TDBEdit;
    dbedtClirg: TDBEdit;
    GroupBox5: TGroupBox;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    DBEdit5: TDBEdit;
    DBEdit6: TDBEdit;
    DBEdit7: TDBEdit;
    DBEdit8: TDBEdit;
    DBEdit9: TDBEdit;
    DBEdit10: TDBEdit;
    DBLookupComboBox1: TDBLookupComboBox;
    GroupBox6: TGroupBox;
    Label14: TLabel;
    Label15: TLabel;
    Label1: TLabel;
    DBEdit12: TDBEdit;
    DBEdit13: TDBEdit;
    DBEdit1: TDBEdit;
    Label4: TLabel;
    Label26: TLabel;
    Label27: TLabel;
    DBEdit23: TDBEdit;
    JvDBDateEdit1: TJvDBDateEdit;
    JvDBComboBox1: TJvDBComboBox;
    btnCancelar: TJvSpeedButton;
    JvDBComboBox2: TJvDBComboBox;
    Label28: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    Label31: TLabel;
    procedure edtCPFChange(Sender: TObject);
    procedure edtNomeChange(Sender: TObject);
    procedure btnFiltrarClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TimerInicioTimer(Sender: TObject);
    procedure btnNovoClick(Sender: TObject);
    procedure btnEditarClick(Sender: TObject);
    procedure btnExcluirClick(Sender: TObject);
    procedure DBRadioGroupTipoClick(Sender: TObject);
    procedure btnOkClick(Sender: TObject);
    procedure TabSheetManutencaoShow(Sender: TObject);
    procedure btnCancelarClick(Sender: TObject);
  private
    { Private declarations }
    procedure FiltrarFornecedores;
    procedure ControlarPageControl(APageIndex: integer);

  public
    { Public declarations }
  end;

var
  frmClientes: TfrmClientes;

implementation

{$R *.dfm}

uses uConexao, uDadosClientes;

{ TfrmClientes }

procedure TfrmClientes.btnEditarClick(Sender: TObject);
begin
  dmDadosClientes.EditarCliente;

  ControlarPageControl(1);
end;

procedure TfrmClientes.btnExcluirClick(Sender: TObject);
begin
  try
    dmDadosClientes.ExcluirCliente;
  except
    on E: Exception do
    begin
      MessageDlg(e.Message, mtError,[mbOK],0);
      FiltrarFornecedores;
    end;
  end;
end;

procedure TfrmClientes.btnFiltrarClick(Sender: TObject);
begin
  FiltrarFornecedores;
end;

procedure TfrmClientes.btnNovoClick(Sender: TObject);
begin
  dmDadosClientes.AdicionarCliente;

  ControlarPageControl(1);
end;

procedure TfrmClientes.btnOkClick(Sender: TObject);
begin
  dmDadosClientes.SalvarManutencao;

  ControlarPageControl(0);
end;

procedure TfrmClientes.ControlarPageControl(APageIndex: integer);
begin
  if APageIndex = 0 then
  begin
    PageControlClientes.Pages[0].TabVisible := true;  // Tab de consulta de clientes
    PageControlClientes.Pages[1].TabVisible := false; // Tab de manutencao de clientes
    PageControlClientes.ActivePageIndex := 0;
    edtCPF.SetFocus;
  end
  else
  begin
    PageControlClientes.Pages[0].TabVisible := false; // Tab de consulta de clientes
    PageControlClientes.Pages[1].TabVisible := true;  // Tab de manutencao de clientes
    PageControlClientes.ActivePageIndex := 1;
  end;
end;

procedure TfrmClientes.DBRadioGroupTipoClick(Sender: TObject);
begin
  try
    dmDadosClientes.cdsTblcliclitip.Value := DBRadioGroupTipo.ItemIndex;

    if DBRadioGroupTipo.ItemIndex = 0 then
    begin
      lblCpfCaption.Caption := 'CPF:';
      lblRgCaption.Caption := 'RG:';
    end
    else
    begin
      lblCpfCaption.Caption := 'CNPJ:';
      lblRgCaption.Caption := 'IE:';
    end;

  finally
    dbedtClicpf.SetFocus;
  end;
end;

procedure TfrmClientes.edtCPFChange(Sender: TObject);
begin
  FiltrarFornecedores;
end;

procedure TfrmClientes.edtNomeChange(Sender: TObject);
begin
  FiltrarFornecedores;
end;

procedure TfrmClientes.FiltrarFornecedores;
var
  SQL: String;
begin

  if trim(edtCPF.Text) <> EmptyStr then
    SQL := ' and clicpf like "%' + trim(edtCPF.Text) + '%" ';

  if Trim(edtNome.Text) <> EmptyStr then
    SQL := SQL + ' and clinome like "%' + trim(edtNome.Text) + '%" ';

  SQL :=
    'select *      ' +
    '  from tblcli ' +
    ' where 1 = 1  ' +
    SQL +
    ' order by clinome';

  dmDadosClientes.FiltrarClientes(SQL);

end;

procedure TfrmClientes.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
  Release;
  frmClientes := nil;
end;

procedure TfrmClientes.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 Then
  begin
    key := #0;
    Perform(Wm_NextDlgCtl,0,0);
  end;
end;

procedure TfrmClientes.btnCancelarClick(Sender: TObject);
begin
  dmDadosClientes.CalcelarManutencao;

  ControlarPageControl(0);
end;

procedure TfrmClientes.TabSheetManutencaoShow(Sender: TObject);
begin
  if not dmDadosClientes.cdsTblcli.Active then
    exit;

  DBRadioGroupTipoClick(DBRadioGroupTipo);
                       {
  if dmDadosClientes.cdsTblcli.State = dsEdit then
  begin
    DBRadioGroupTipo.Enabled := false;
    dbedtClicpf.ReadOnly := true;
    dbedtClicpf.Color := clBtnFace;

    DBEdit3.SetFocus;
  end
  else if dmDadosClientes.cdsTblcli.State = dsInsert then
  begin
    DBRadioGroupTipo.Enabled := true;
    dbedtClicpf.ReadOnly := false;
    dbedtClicpf.Color := clWindow;

    dbedtClicpf.SetFocus;
  end;
}
  dbedtClicpf.SetFocus;
end;

procedure TfrmClientes.TimerInicioTimer(Sender: TObject);
begin
  TimerInicio.Enabled := false;

  ControlarPageControl(0);

  frmClientes.WindowState := wsMaximized;
end;

end.
